import 'package:car_music/src/modules/eq/chat_page.dart';
import 'package:car_music/src/modules/eq/my_ed_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../commont/button.dart';
import '../../commont/group_ch.dart';
import '../../commont/kcontainer.dart';
import '../../tool/assets.dart';

class MyEq extends EqPageDelegate<MyEdController> {
  final PageController _pageController = PageController();
  MyEq({super.key});

  @override
  void onClose() {
    _pageController.dispose();
    super.onClose();
  }

  ///圆点pagecontroller
  Widget _pointItem() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(2, (index) {
        return Obx(() {
          bool iscurrent = controller.currentPage.value == index;
          return AnimatedContainer(
            duration: const Duration(milliseconds: 300),
            margin: const EdgeInsets.symmetric(horizontal: 4.0),
            width: iscurrent ? 8 : 8,
            height: 8.0,
            decoration: BoxDecoration(
              color: iscurrent ? Color_select_btn : Color_bg_btn,
              borderRadius: BorderRadius.circular(4.0),
              border: Border.all(color: Color_select_btn, width: iscurrent ? 0 : 0.8),
            ),
          );
        });
      }),
    );
  }

  ///pageView
  Widget _pageView() {
    return Obx(() {
      final ch = globalController.rxModel.value.current_ch;
      final signlist = [ch.sign_list.sublist(0, 8), ch.sign_list.sublist(8, 15)]; //当前通道，两页信号数组
      return PageView(
        controller: _pageController,
        physics: const ClampingScrollPhysics(), // 禁止弹性滚动
        onPageChanged: (index) => controller.currentPage.value = index,
        children: signlist.map(($0) {
          return Container(
            padding: const EdgeInsets.only(left: 30, right: 30, top: 5, bottom: 5),
            child: Row(
                spacing: 5,
                children: $0.map(($1) {
                  return Flexible(
                    child: Column(
                      spacing: 5,
                      children: [
                        ///gain滑动块
                        Flexible(
                          flex: 8,
                          child: kSlider(
                            dirction: Axis.horizontal,
                            value: $1.gain,
                            min: $1.min_gain,
                            max: $1.max_gain,
                            onChanged: (p0) => globalController.updateSign(true, $1, p0),
                            onChangedForSend: (p0) => globalController.sendEQ($1),
                          ),
                        ),

                        ///gain增益
                        Flexible(child: kMyTextButton(textColor: Colors.white, name: '${$1.gain.roundDouble(1)}')),

                        ///freq频率
                        Flexible(
                            child: kMyTextButton(
                          name: '${$1.freq}',
                          select: $1.select,
                          onClick: (p0) {},
                        )),
                      ],
                    ),
                  );
                }).toList()),
          );
        }).toList(),
      );
    });
  }

  ///q值滑动块
  Widget _qualitySlider() {
    return Obx(() {
      final ch = globalController.rxModel.value.current_ch;
      return Row(
        spacing: 5,
        children: [
          const SizedBox(width: 30, child: Text('Q:', textAlign: TextAlign.right)),
          SizedBox(
            width: 60,
            height: 25,
            child: kMyTextButton(textColor: Colors.white, name: '${ch.select_sign.Q.roundDouble(1)}'),
          ),
          Flexible(
            child: kSlider(
              value: ch.select_sign.Q,
              dirction: Axis.vertical,
              min: ch.select_sign.min_Q,
              max: ch.select_sign.max_Q,
              onChanged: ($0) => globalController.updateSign(false, ch.select_sign, $0),
              onChangedForSend: (p0) => globalController.sendEQ(ch.select_sign),
            ),
          ),
          const SizedBox(width: 30)
        ],
      );
    });
  }

  Widget _resetList() {
    return Obx(() {
      return Row(
        spacing: 10,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: List.generate(controller.btnTypes.length, (index) {
          return SizedBox(
            width: 100,
            child: kMyTextButton(
              name: controller.btnTypes[index],
              select: controller.typeIndex.value == index,
              onClick: (p) {
                switch (p) {
                  case 'RESTORE':
                  case 'BYPASS':
                    controller.typeIndex.value = p == 'RESTORE' ? index : 10;
                    globalController.bypassEQ();
                    break;
                  case 'RESET CH':
                    globalController.resetEQ(all: false);
                    break;
                  case 'RESET ALL':
                    globalController.resetEQ();
                    break;
                  default:
                }
              },
            ),
          );
        }).toList(),
      );
    });
  }

  @override
  Widget buildBody(BuildContext context, Widget child) {
    Get.lazyPut(() => MyEdController());
    return Column(
      children: [
        ///图形表
        ChartPage(flex: 6),

        ///控制页面的pagecontroller
        Expanded(flex: 1, child: _pointItem()),

        ///中间的eq控制
        Flexible(
          flex: 10,
          child: kContainer(
            child: Column(
              children: [
                Flexible(flex: 9, child: _pageView()),
                Flexible(flex: 2, child: _qualitySlider()),
              ],
            ),
          ),
        ),

        ///间隔
        const SizedBox(height: 10),

        ///底部eq通道设置
        Flexible(
          flex: 4,
          child: kContainer(
            padding: const EdgeInsets.only(left: 10, right: 10),
            child: Column(
              spacing: 10,
              children: [
                Flexible(flex: 5, child: ChGroupView(title: 'EQ', groupList: controller.groupList)),
                Flexible(flex: 2, child: _resetList()),
                const SizedBox(height: 5),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
